//
//  NSNumber+QWExtension.m
//  WJCommon-master
//
//  Created by Yunhai.Wu on 15/12/16.
//  Copyright © 2015年 WJ. All rights reserved.
//

#import "NSNumber+QWExtension.h"

@implementation NSNumber (QWExtension)

- (void) qw_times:(void (^)(void))block {
    for (int i = 0; i < self.integerValue; i++)
        block();
}

- (void) qw_timesWithIndex:(void (^)(NSUInteger))block {
    for (int i = 0; i < self.unsignedIntegerValue; i++)
        block(i);
}

- (void) qw_upto:(int)number do:(void (^)(NSInteger))block {
    for (NSInteger i = self.integerValue; i <= number; i++)
        block(i);
}

- (void) qw_downto:(int)number do:(void (^)(NSInteger))block {
    for (NSInteger i = self.integerValue; i >= number; i--)
        block(i);
}

- (NSNumber *) qw_second {
    return self.qw_seconds;
}

- (NSNumber *) qw_seconds {
    return self;
}

- (NSNumber *) qw_minute {
    return self.qw_minutes;
}

- (NSNumber *) qw_minutes {
    return @(self.floatValue * 60);
}

- (NSNumber *)qw_hour {
    return self.qw_hours;
}

- (NSNumber *)qw_hours {
    return @(self.floatValue * [@60 qw_minutes].integerValue);
}

- (NSNumber *)qw_day {
    return self.qw_days;
}

- (NSNumber *)qw_days {
    return @(self.floatValue * [@24 qw_hours].integerValue);
}

- (NSNumber *)qw_week {
    return self.qw_weeks;
}

- (NSNumber *)qw_weeks {
    return @(self.floatValue * [@7 qw_days].integerValue);
}

- (NSNumber *)qw_fortnight {
    return self.qw_fortnights;
}

- (NSNumber *)qw_fortnights {
    return @(self.floatValue * [@2 qw_weeks].integerValue);
}

- (NSNumber *)qw_month {
    return self.qw_months;
}

- (NSNumber *)qw_months {
    return @(self.floatValue * [@30 qw_days].integerValue);
}

- (NSNumber *)qw_year {
    return self.qw_years;
}

- (NSNumber *)qw_years {
    return @(self.floatValue * [@(365.25) qw_days].integerValue);
}

- (NSDate *)qw_ago {
    return [self qw_ago:[NSDate date]];
}

- (NSDate *)qw_ago:(NSDate *)time {
    return [NSDate dateWithTimeIntervalSince1970:([time timeIntervalSince1970] - self.floatValue)];
}

- (NSDate *)qw_since:(NSDate *)time {
    return [NSDate dateWithTimeIntervalSince1970:([time timeIntervalSince1970] + self.floatValue)];
}

- (NSDate *)qw_until:(NSDate *)time {
    return [self qw_ago:time];
}

- (NSDate *)qw_fromNow {
    return [self qw_since:[NSDate date]];
}

@end
